package com.bringway.global.dao.impl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.core.namedparam.MapSqlParameterSource;
import org.springframework.jdbc.core.namedparam.SqlParameterSource;
import org.springframework.stereotype.Repository;

import com.bringway.framework.dao.BaseJdbcDao;
import com.bringway.framework.dao.ZhfwJdbcDao;
import com.bringway.global.dao.UserDao;
import com.bringway.global.domain.SysUserDetails;
import com.bringway.global.domain.User;
import com.bringway.global.interceptor.JspAuthorityFilter;
import com.bringway.global.util.UserUtil;

import net.sf.json.JSONObject;

/*
* @author 作者 xuef
* @version 创建时间：2015年9月14日 下午3:55:42
* 类说明
*/
@Repository
public class UserDaoImpl extends BaseJdbcDao implements UserDao {
	
	Logger logger = Logger.getLogger(UserDaoImpl.class);
	
	private class UserMapper implements RowMapper{

		@Override
		public Object mapRow(ResultSet rs, int rowNum) throws SQLException {
			User user = new User();
			user.setId(rs.getString("ID"));
			user.setGyh(rs.getString("GYH"));
			user.setGyh(rs.getString("SJHM"));
			user.setPassword(rs.getString("USER_PASSWORD"));
			user.setUsername(rs.getString("USER_NAME"));
			user.setRoleId(rs.getString("ROLE_ID"));
			user.setRoleName(rs.getString("GWMC"));
			return user;
		}
		
		
	}

	@SuppressWarnings("unchecked")
	@Override
	public User login(String gyh, String password) throws Exception {
		return null;
	}

	@SuppressWarnings("unchecked")
	@Override
	public SysUserDetails login(String gyh) {
		return null;
	}


	@Override
	public List<Map<String, Object>> queryRoleList(JSONObject params) {
		return null;
	}

	@Override
	public boolean isHasAuthrity(String user_realId, String user_roleId, String pageId) {
		StringBuffer sql1 = new StringBuffer();
		Map<String,String> paramMap = new HashMap<>();
		paramMap.put("USER_ID", user_realId);
		paramMap.put("ROLE_ID", user_roleId);
		paramMap.put("MENU_ID", pageId);
		sql1.append(" SELECT COUNT(*) from t_fm_sys_user_menu where user_id = :USER_ID and menu_id= :MENU_ID ");
		logger.info("isHasAuthrity sql1 is : " + sql1.toString());
		logger.info("isHasAuthrity paramMap is : " + paramMap.toString());
		int ct1 = this.getNamedParameterJdbcTemplate().queryForObject(sql1.toString(), paramMap, Integer.class);
		if(ct1 > 0 ){
			return true;
		}else{
			StringBuffer sql2 = new StringBuffer();
			sql2.append(" SELECT COUNT(*) FROM t_fm_sys_menu_role where role_id=:ROLE_ID and menu_id=:MENU_ID ");
			logger.info("isHasAuthrity sql2 is : " + sql2.toString());
			int ct2 = this.getNamedParameterJdbcTemplate().queryForObject(sql2.toString(), paramMap, Integer.class);
			if(ct2 > 0){
				return true;
			}
		}
		return false;
	}
	
	
}
